using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class EmployeesEF
	{
		public EmployeesEF(DbContext dbContext)
		{
			employeesServerEF = new EmployeesServerEF(dbContext);
            sysCarShopsServerEF = new SysCarShopsServerEF(dbContext);
            sysAccountsServerEF = new SysAccountsServerEF(dbContext);
        }
		public EmployeesServerEF employeesServerEF { get; set; }
        public SysCarShopsServerEF sysCarShopsServerEF { get; set; }
        public SysAccountsServerEF sysAccountsServerEF { get; set; }
    }
	[InjectClass]
	public class EmployeesServer : IEmployeesServer
	{
		private readonly EmployeesEF _employeesEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public EmployeesServer(DbContext dbContext)
		{
			_employeesEF = new EmployeesEF(dbContext);
		}
		public async Task<ResponseApiDto> AddAsync(RequestEmployeesDto dto)
		{
			string cid=await _employeesEF.sysCarShopsServerEF.Query(d=>d.CsAccount==dto.carShopId).Select(d=>d.CsId).FirstOrDefaultAsync();

            //创建员工账号
            string salt = Tools.Guid_1();
            SysAccounts newAccount = new SysAccounts
            {
                AAccount = dto.account,
                AName = dto.name,
                ARole = "employee",
                ASalt = salt,
                APassword = Encrypt.GetMd5Hash(dto.powhg + salt)
            };
            await _employeesEF.sysAccountsServerEF.AddAsync(newAccount);
                //创建员工表
                Employees employees = new Employees
                {
                    EAccountId = newAccount.AId,
                    ECarShopId = cid,
                    EName = dto.name,
                    EGender = dto.gender,
                    EBirthDate = dto.birthDate,
                    EIdCardNumber = dto.idCardNumber,
                    EPhoneNumber = dto.phoneNumber,
                    EEmail = dto.email,
                    EAddress = dto.address,
                    EEmergencyContact = dto.emergencyContact,
                    EPosition = dto.position,
                    EHireDate = dto.hireDate,
                    EStatus = dto.status,
                    ERemark = dto.remark,
                };

                return ResponseApiDto.Create(await _employeesEF.employeesServerEF.AddAsync(employees));
       
        }
		public async Task<ResponseApiDto> DeleteAsync(RequestEmployeesDto dto)
		{
			return ResponseApiDto.Create(await _employeesEF.employeesServerEF.DeleteAsync(dto.ToDeleteEntity()));
		}
		public async Task<ResponseApiDto> UpdateAsync(RequestEmployeesDto dto)
		{
			return ResponseApiDto.Create(await _employeesEF.employeesServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
		}
		public async Task<ResponseApiDto> PaginationAsync(int page, int limit)
		{
			int total = 0;
			List<ResponseEmployeesDto> data = new List<ResponseEmployeesDto>();
			List<Employees> employeess = await _employeesEF.employeesServerEF.Pagination1(d=>true,false, d => d.ECreatedAt, out total, page, limit).ToListAsync();
			foreach (var employees in employeess)
			{
				data.Add(employees.ToResponseDto());
			}
			return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total=total,data=data});
		}
        /// <summary>
        /// 获取员工名称
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_em_name(string uid)
        {
			var data = await _employeesEF.employeesServerEF.Query(d => d.EAccountId == uid).Select(d =>new
			{
				name=d.EName,
				id=d.EId
			}).SingleOrDefaultAsync();
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto {  data = data });
        }
        /// <summary>
        /// 获取汽车店员工消息
        /// </summary>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_employ(int page, int limit, string key, string uid)
        {
            int total = 0;
            key = key ?? "";
            string cid = await _employeesEF.sysCarShopsServerEF.Query(d => d.CsAccount == uid).Select(d => d.CsId).FirstOrDefaultAsync();
            List<Employees> employeess = await _employeesEF.employeesServerEF.Pagination1(d => d.EName.Contains(key)&&d.ECarShopId==cid, false, d => d.ECreatedAt, out total, page, limit).ToListAsync();
            List<object> data = new List<object>();
            foreach (var employees in employeess)
            {
                data.Add(new
                {
                    id=employees.EId,
                    name=employees.EName,
                    position=employees.EPosition,
                    number=employees.EPhoneNumber,
                    address=employees.EAddress,
                });
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });

        }
    }
}
